#!/usr/bin/env python
# -*- coding: utf-8 -*-


import requests
import json


def get_access_token():
    """
    使用 API Key，Secret Key 获取access_token，替换下列示例中的应用API Key、应用Secret Key
    """

    # url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=[应用API Key]&client_secret=[应用Secret Key]"
    url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=egOv4wHLaIG6UvZ66SWK1KYw&client_secret=tu6GMCCM58uLbd98daheWfRstrGPLuHS"

    payload = json.dumps("")
    headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}

    response = requests.request("POST", url, headers=headers, data=payload)
    token = response.json().get("access_token")
    print(token)
    # 24.a1e8989d218228b5c54241359dbff1db.2592000.1694835587.282335-37823141
    return token


def main():
    """
    替换下列示例中的申请发布时填写的API名称
    """

    token = "24.a1e8989d218228b5c54241359dbff1db.2592000.1694835587.282335-37823141"
    # url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/txt2img/[申请发布时填写的API名称]?access_token=" + get_access_token()
    url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/txt2img/talk_01?access_token=" + get_access_token()
    text = "一周菜谱"
    url = "https://aip.baidubce.com/rest/2.0/wenxinworkshop/api/v1/template/info?access_token=" + get_access_token() + "&id=3832&text=" + text
    #
    payload = json.dumps(
        {"width": 512, "height": 512, "prompt": " < lora:tiantian - 01: 0.9 >,1girl, hat, solo, jewel", })
    headers = {'Content-Type': 'application/json'}

    response = requests.request("GET", url, headers=headers, data="")

    print(response.text)


if __name__ == '__main__':
    # get_access_token()
    main()

